﻿using DotNetCore.CAP;

namespace Service.CAP.EventBus
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="capPublisher"></param>
    public class CapPublisher(ICapPublisher capPublisher) : IEventPublisher
    {
        public virtual async Task PublishDelayAsync<T>(TimeSpan timeSpan, T eventObj, string callbackName = null,
            CancellationToken cancellationToken = default(CancellationToken))
            where T : IEvent
        {
            var eventInstance = (IEvent)eventObj;
            await capPublisher.PublishDelayAsync(timeSpan, eventInstance.EventName, eventObj, callbackName, cancellationToken);
        }

        public virtual async Task PublishDelayAsync<T>(TimeSpan timeSpan, T eventObj,
            IDictionary<string, string> headers, CancellationToken cancellationToken = default(CancellationToken))
            where T : IEvent
        {
            var eventInstance = (IEvent)eventObj;
            await capPublisher.PublishDelayAsync<T>(timeSpan, eventInstance.EventName, eventObj, headers, cancellationToken);
        }

        public virtual async Task PublishAsync<T>(T eventObj, string callbackName = null,
            CancellationToken cancellationToken = default(CancellationToken))
            where T : IEvent
        {
            var eventInstance = (IEvent)eventObj;
            await capPublisher.PublishAsync(eventInstance.EventName, eventObj, callbackName, cancellationToken);
        }

        public virtual async Task PublishAsync<T>(T eventObj, IDictionary<string, string> headers,
            CancellationToken cancellationToken = default(CancellationToken))
            where T : IEvent
        {
            var eventInstance = (IEvent)eventObj;
            await capPublisher.PublishAsync<T>(eventInstance.EventName, eventObj, headers, cancellationToken);
        }

        public virtual void Publish<T>(T eventObj, string callbackName = null)
            where T : IEvent
        {
            var eventInstance = (IEvent)eventObj;
            capPublisher.Publish(eventInstance.EventName, eventObj, callbackName);
        }

        public virtual void Publish<T>(T eventObj, IDictionary<string, string> headers)
            where T : IEvent
        {
            var eventInstance = (IEvent)eventObj;
            capPublisher.Publish(eventInstance.EventName, eventObj, headers);
        }
    }
}